package com.example.demo.utils;

import java.util.Calendar;
import java.util.Date;

/**
 * @author FENGYU
 * @description
 * @date 2023/8/31 9:42
 */
public class DateTimeUtil {
    public static void main(String[] args) {
        //System.out.println(  DateTimeUtil.addMonths(DateTimeUtil.getCurrentMonth(), -1));
        String str = "a,b,c,";
        int lastIndex = str.lastIndexOf(",");
        str = str.substring(0,str.lastIndexOf(","));
        System.out.println(str);
    }

    public static Date addMonths(Date date, int amount)
    {
        Calendar tmpDate = newCalendar(date);
        tmpDate.add(Calendar.MONTH, amount);
        return tmpDate.getTime();
    }

    public static Calendar newCalendar(Date date)
    {
        Calendar calender = Calendar.getInstance();
        calender.setTime(date);
        return calender;
    }

    public static Date getCurrentMonth()
    {
        Date currentDate = getCurrentTime();
        return getDateTime(currentDate, null, null, 1, 0, 0, 0);
    }
    public static Date getCurrentTime()
    {
        // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // try {
        // Date date = sdf.parse("2015-05-14 09:51:00");
        // return date;
        // } catch (ParseException e) {
        // // TODO Auto-generated catch block
        // e.printStackTrace();
        // }
        return new Date();
    }
    public static Date getDateTime(Date date, Integer year, Integer month, Integer day, Integer hour, Integer min,
                                   Integer second)
    {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.set(year == null ? cal.get(Calendar.YEAR) : year, month == null ? cal.get(Calendar.MONTH) : month,
                day == null ? cal.get(Calendar.DATE) : day, hour == null ? cal.get(Calendar.HOUR_OF_DAY) : hour,
                min == null ? cal.get(Calendar.MINUTE) : min, second == null ? cal.get(Calendar.SECOND) : second);

        return cal.getTime();
    }
}
